package com.example.demo.mq.consumer;

import com.example.demo.entity.Message;
import com.example.demo.mapper.user.MessageMapper;
import com.example.demo.util.EmptyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;


@Component
@RabbitListener(queues = "demoQueue")
public class RabbitConsumer {
    private static final Logger logger = LoggerFactory.getLogger(RabbitConsumer.class);

    @Autowired
    private MessageMapper messageMapper;

    /**
     * 消息消费
     * @RabbitHandler 代表此方法为接收到消息后的处理方法
     */
    //@RabbitHandler
    public void recieved(String msg){
        System.out.println("接收到队列[demoQueue]的消息：" + msg);
        if(EmptyUtil.isEmpty(msg)){
            return;
        }
        Message message = new Message();
        message.setInsertBy("system");
        message.setInsertTime(new Date());
        message.setUpdateBy("system");
        message.setUpdateTime(new Date());
        message.setUserId(1);
        message.setContent(msg);
        message.setStatus(1);
        messageMapper.insert(message);
    }
}
